//
//  MTTabBarController.m
//  ebook
//
//  Created by 吴超飞 on 16/1/14.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "MTTabBarController.h"
#import "MTNavigationController.h"
#import "MTHomeViewController.h"
#import "MTStoreViewController.h"
#import "MTMineViewController.h"
#import "UIImage+RTTint.h"
#import "BaiduMobStat.h"
#import "CatchCrash.h"
#import "EbookNetRequest.h"
#import "GuestLoginManager.h"
#import "StoreBaseViewController.h"
#import "MobCpuInfoViewController.h"

@interface MTTabBarController ()

@end

@implementation MTTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.tabBar.translucent = NO;
    
    NSInteger thisTime = [[[NSUserDefaults standardUserDefaults] objectForKey:kLauchTimes] integerValue]+1;
    [[NSUserDefaults standardUserDefaults] setObject:@(thisTime) forKey:kLauchTimes];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    //重新启动时清除UIWebView的缓存
    [[NSURLCache  sharedURLCache ] removeAllCachedResponses];
    
    // 设置子控制器
    [self addChildVcs];
    
    //注册消息处理函数的处理方法
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    MTLog(@"%@",PATH_OF_DOCUMENT);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
}

- (void)addChildVcs
{
    // 添加首页控制器
    MTHomeViewController *homeVc = [[MTHomeViewController alloc] init];
    [self setChildVc:homeVc title:@"书架" imageName:@"tabbar_book_shelf" selectedImageName:@"tabbar_book_shelf_hl"];
    
    NSDictionary *dict = kBookCityDataDict;
    int openmenustore = [dict[@"data"][@"openmenustore"] intValue];
    if(openmenustore == 1){
        // 添加精选控制器
        MTStoreViewController *bookCityVc = [[MTStoreViewController alloc] initWithTitle:@"精选" tabTitle:@"精选" urlKey:@"menustore"];
        [self setChildVc:bookCityVc title:@"精选" imageName:@"tabbar_book_library" selectedImageName:@"tabbar_book_library_hl"];
    }
    
    // 添加书城控制器
    MTStoreViewController *markVc = [[MTStoreViewController alloc] initWithTitle:@"书城" tabTitle:@"书城" urlKey:@"menu"];
    [self setChildVc:markVc title:@"书城" imageName:@"tabbar_jingxuan_n" selectedImageName:@"tabbar_jingxuan_s"];
    
    int openUnion = [dict[@"data"][@"ad"][@"baidu_neirong"][@"open"] intValue];
    if(openUnion == 1){
        // 添加内容联盟控制器
        MobCpuInfoViewController *infoVc = [[MobCpuInfoViewController alloc] init];
        [self setChildVc:infoVc title:@"发现" imageName:@"tabbar_discovery_n" selectedImageName:@"tabbar_discovery_s"];
    }
    
    // 添加我的控制器
    MTMineViewController *mineVc = [[MTMineViewController alloc] init];
    [self setChildVc:mineVc title:@"我的" imageName:@"tabbar_pandora_box" selectedImageName:@"tabbar_pandora_box_hl"];
}

- (void)setChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    childVc.tabBarItem.title = title;
    
    // 设置tabBar的图片
    //    childVc.tabBarItem.image = [[UIImage imageNamed:imageName] rt_tintedImageWithColor:MTConfigColor(@"MTTabbar_normalImg_Color")] ;
    UIImage *normal = [[[UIImage imageNamed:imageName] rt_tintedImageWithColor:MTConfigColor(@"MTTabbar_normalImg_Color")] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *sel = [[[UIImage imageNamed:selectedImageName] rt_tintedImageWithColor:MTConfigColor(@"MTTabbar_selectedImg_Color")] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.image = normal;
    childVc.tabBarItem.selectedImage = sel;
    
    // 设置tabBar的文字
    [childVc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10], NSForegroundColorAttributeName:MTConfigColor(@"MTTabbar_normalTitle_Color")} forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10], NSForegroundColorAttributeName:MTConfigColor(@"MTTabbar_selectedTitle_Color")} forState:UIControlStateSelected];
    
    MTNavigationController *navVc = [[MTNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:navVc];
}

- (BOOL)shouldAutorotate {
    
    if (!isIpad) {
        
        // 获取当前的控制器
        UINavigationController *navC = self.selectedViewController;
        UIViewController *currentVC = navC.visibleViewController;
        // 因为默认shouldAutorotate是YES，所以每个不需要支持横屏的控制器都需要重写一遍这个方法
        // 一般项目中要支持横屏的界面比较少，为了解决这个问题，就取反值：shouldAutorotate返回为YES的时候不能旋转，返回NO的时候可以旋转
        // 所以只要重写了shouldAutorotate方法的控制器，并return了NO，这个控制器就可以旋转
        // 当然，如果项目中支持横屏的界面占多数的话，可以不取反值。
        //    MTLog(@"当前控制器：%@  是否支持旋转：%zd", currentVC, !currentVC.shouldAutorotate);
        
        return !currentVC.shouldAutorotate;
    } else {
        
        return [ReaderGlobalModel shareModel].readerConfig.ipadIsSupportLandscape;
    }
}

@end
